/**
 * @author Richie
 * @constructor 
 */
DateUtils = function () {
	/**
	 * <b>民國日期轉西元日期</b>
	 * @param 	origDate	民國日期
	 * @param 	dateSplit 	日期分隔符號
	 * @return 	西元日期型態
	 */
	this.cDateToWDate = function (origDate, dateSplit) {
		var result = "";
		if ((origDate != null) && (origDate != "")) {
			var date = origDate;
			if(dateSplit != "none"){
				date = replaceAll(origDate, dateSplit, "");
			}
			date = lPad(date, 7, "0");
			var dateYear = String(Number(date.substring(0, 3)) + 1911);
			if(dateSplit != "none"){
				result = dateYear + dateSplit + date.substring(3, 5) + dateSplit + date.substring(5, 7);
			}else{
				result = dateYear + date.substring(3, 5) + date.substring(5, 7);
			}
		}
		return result;
	};

	/**
	 * <b>西元日期轉民國日期</b>
	 * @param 	origDate	西元日期
	 * @param 	dateSplit 	日期分隔符號
	 * @return 	民國日期
	 */
	this.wDateToCDate = function (origDate, dateSplit) {
		var result = "";
		if ((origDate != null) && (origDate != "")) {
			var date = origDate;
			if(dateSplit != "none"){
				date = replaceAll(origDate, dateSplit, "");
			}
			date = String(Number(date) - 19110000);
			date = lPad(date, 7, "0");
			if(dateSplit != "none"){
				result = date.substring(0, 3) + dateSplit + date.substring(3, 5) + dateSplit + date.substring(5, 7);
			}else{
				result = date.substring(0, 3) + date.substring(3, 5) + date.substring(5, 7);
			}
		}
		return result;
	};

	/**
	 * <b>取得目前的日期時間</b>
	 * @param 	dateType	日期型態可分為AD(西元年)以及ROC(民國年)，預設為AD<br/>
	 * @return 	DateObject物件
	 * 			<li>dateObject.getYear			年</li>
	 * 			<li>dateObject.getMonth 		月</li>
	 * 			<li>dateObject.getDay			日</li>
	 * 			<li>dateObject.getHour 			小時</li>
	 * 			<li>dateObject.getMinute		分鐘</li>
	 * 			<li>dateObject.getSecond 		秒</li>
	 * 			<li>dateObject.getMillisecond 	毫秒</li>
	 */
	this.getNowDateTime = function (dateType) {
		var result = new Object();
		var nowDate = new Date();
		var year = nowDate.getFullYear();
		var month = String(nowDate.getMonth() + 1);
		var day = String(nowDate.getDate());
		var hour = String(nowDate.getHours());
		var minute = String(nowDate.getMinutes());
		var second = String(nowDate.getSeconds());
		var millisecond = String(nowDate.getMilliseconds());

		if ( dateType == "ROC"){
			year = String(Number(year) - 1911);
			if (year.length < 3) {
				year = "0" + year;
			}
		}

		while (month.length < 2) {
			month = "0" + month;
		}

		while (day.length < 2) {
			day = "0" + day;
		}

		while (hour.length < 2) {
			hour = "0" + hour;
		}

		while (minute.length < 2) {
			minute = "0" + minute;
		}


		while (second.length < 2) {
			second = "0" + second;
		}


		while (millisecond.length < 3) {
			millisecond = "0" + millisecond;
		}

		result.getYear = year;
		result.getMonth = month;
		result.getDay = day;
		result.getHour = hour;
		result.getMinute = minute;
		result.getSecond = second;
		result.getMillisecond = millisecond;

		return result;
	};

	/**
	 * <b>取得系統時間</b><br/>
	 * 如：getSysTime("ROC", "/" , true, "", true, true) -> 100/06/21 23:12:17.123<br/>
	 * @param	dateType 		日期型態可分為AD(西元年)以及ROC(民國年)，預設為AD
	 * @param 	dateSplit 		日期分隔符號
	 * @param 	showTime 		顯示時間與否
	 * @param 	connectionSplit 日期時間分隔符號
	 * @param 	showTimeSplit 	顯示時間分隔符號與否
	 * @param 	showMillisecond 顯示毫秒與否
	 * @return 	目前的日期時間
	 */
	this.getSysTime = function (dateType, dateSplit, showTime, connectionSplit, showTimeSplit, showMillisecond) {
		var result = "";
		var nowDate = new Date();
		var year = nowDate.getFullYear();
		var month = String(nowDate.getMonth() + 1);
		var day = String(nowDate.getDate());
		var hour = String(nowDate.getHours());
		var minute = String(nowDate.getMinutes());
		var second = String(nowDate.getSeconds());
		var millisecond = String(nowDate.getMilliseconds());

		while (month.length < 2) {
			month = "0" + month;
		}

		while (day.length < 2) {
			day = "0" + day;
		}

		while (hour.length < 2) {
			hour = "0" + hour;
		}

		while (minute.length < 2) {
			minute = "0" + minute;
		}


		while (second.length < 2) {
			second = "0" + second;
		}


		while (millisecond.length < 3) {
			millisecond = "0" + millisecond;
		}
		var date = year + "/" + month + "/" + day;
		if (dateType == "ROC") {
			date = this.wDateToCDate(date, "/");
		}

		if (dateSplit == "none") {
			date = date.replace(/\//g, "");
		} else if (!((dateSplit == "none") || (dateSplit == ""))) {
			date = date.replace(/\//g, dateSplit);
		}

		if (showTime == true) {
			var time = "";
			if (showTimeSplit == true) {
				time = hour + ":" + minute + ":" + second;
				if (showMillisecond == true) {
					time = time + "." + millisecond;
				}
			} else {
				time = hour + minute + second;
				if (showMillisecond == true) {
					time = time + millisecond;
				}
			}

			if (connectionSplit == "none") {
				result = date + time;
			} else if (!((connectionSplit == "none") || (connectionSplit == ""))) {
				result = date + connectionSplit + time;
			} else {
				result = date + " " + time;
			}
		} else {
			result = date;
		}
		return result;
	};

	/**
	 * <b>民國日期加減</b>
	 * @param origDate	民國日期
	 * @param calcType	欲加減之日期型別：day(預設), week, month, year
	 * @param num	欲增減數目
	 * @return	計算完畢之日期
	 */
	this.calcCDate = function (origDate, dateSplit, calcType, num){
		var result = "";
		if(origDate != ""){
			result = this.wDateToCDate(this.calcWDate(this.cDateToWDate(origDate, dateSplit), dateSplit, calcType, num), dateSplit);
		}
		return result;
	};

	/**
	 * <b>西元日期加減</b>
	 * @param origDate	西元日期
	 * @param calcType	欲加減之日期型別：day(預設), week, month, year
	 * @param num	欲增減數目
	 * @return	計算完畢之日期
	 */
	this.calcWDate = function (origDate, dateSplit, calcType, num){
		var result = "";
		if(origDate != ""){
			var dateObj = new Date(origDate);
			num = Number(num);
			switch (calcType){
			case "day":
				dateObj = new Date(Date.parse(dateObj) + 86400000*num);
				break;
			case "week":
				dateObj = new Date(Date.parse(dateObj) + 86400000*7*num);
				break;
			case "month":
				dateObj = new Date(dateObj.getFullYear(), dateObj.getMonth()+num, dateObj.getDate());
				break;
			case "year":
				dateObj = new Date(dateObj.getFullYear()+num, dateObj.getMonth(), dateObj.getDate());
				break;
			default :
				break;
			}

			var year = dateObj.getFullYear();
			var month = String(dateObj.getMonth() + 1);
			var day = String(dateObj.getDate());

			while (month.length < 2) {
				month = "0" + month;
			}

			while (day.length < 2) {
				day = "0" + day;
			}

			result =  year + dateSplit + month + dateSplit + day;
		}
		return result;
	};

	/**
	 * <b>取得民國某年某月的天數</b>
	 * @param	year	民國年
	 * @param	month 	月份
	 */
	this.cDaysOfMonth = function (year, month){
		var result = "";
		if ((year != "") && (month != "") && (Number(year) > 0) && (Number(month) >= 1) && (Number(month) <= 12)) {
			result = this.wDaysOfMonth(Number(year)+1911, Number(month));
		}
		return result;
	};

	/**
	 * <b>取得西元某年某月的天數</b>
	 * @param	year	西元年
	 * @param	month 	月份
	 * @return 	天數
	 */
	this.wDaysOfMonth = function (year, month){
		var result = "";
		if ((year != "") && (month != "") && (Number(year) > 0) && (Number(month) >= 1) && (Number(month) <= 12)) {
			var leapYear = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
			var notLeapYear = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

			if(isInteger(year) && (year%4 == 0 && year%100 != 0 || year%400 == 0)){
				result = leapYear[month-1];
			}else{
				result = notLeapYear[month-1];
			}
		}
		return result;
	};
	
	/**
	 * <b>日期輸入格式檢查</b><br/>
	 * 使用方法：onkeyup="DateUtil.setDateConstraint(event, this, 'AD', '/')"
	 * @param	event		事件
	 * @param	obj			檢查物件
	 * @param	dateType	日期型態
	 * @return：字元
	 */
	this.setDateConstraint = function (event, obj, dateType, dateSplit){
		var keyCode = whichKeyPress(event);
		if(!((keyCode == 37) || (keyCode == 39) || (keyCode == 8))){
			obj.value = obj.value.replace(/[^0-9\/]/g,'');
			var value = obj.value;
			var temp1 = "";
			var temp2 = "";
			var temp3 = "";
			var temp4 = "";
			var temp5 = "";		

			if(dateType == "AD"){
				if(value.length < 4){
					obj.value = value.replace(/[^0-9]/g,'');
					return;
				}else{
					obj.value = value.replace(/[^0-9\/]/g,'');
				}

				if(value.length < 4){
					value = value.replace(/[^0-9]/g,'');
					temp1 = obj.value;
				}else{
					temp1 = value.substring(0, 4);
					temp1 = temp1.replace(/[^0-9]/g,'');
					if(value.length >= 5){
						temp2 = value.substring(4, 5);
						temp2 = temp2.replace(/[^\/]/g,'');
					}
					if(value.length >= 6 ){
						if(value.length ==6 ){
							temp3 = value.substring(5, 6);
						}else{
							temp3 = value.substring(5, 7);
						}
						temp3 = temp3.replace(/[^0-9]/g,'');
					}
					if(value.length >=8 ){
						temp4 = value.substring(7, 8);
						temp4 = temp4.replace(/[^\/]/g,'');
					}
					if(value.length > 8 ){
						temp5 = value.substring(8);
						temp5 = temp5.replace(/[^0-9]/g,'');
					}
				}
				value = temp1 + temp2 + temp3 + temp4 + temp5 ;
				obj.value = value;
				if(value.length >= 4){
					if(value.length == 4){
						obj.value = value + dateSplit;
					}
					if(value.length == 7){
						obj.value = value + dateSplit;
					}
				}
			}else if(dateType=="ROC"){
				if(value.length < 3){
					obj.value = value.replace(/[^0-9]/g,'');
					return;
				}else{
					obj.value = value.replace(/[^0-9\/]/g,'');
				}

				if((value.length == 1)&&(value != "1")&&(value != "0")){
					obj.value = "0" + value;
					return;
				}

				if((value.length == 3)&&(keyCode == 111)){
					obj.value = "0" + value;
					return;
				}

				if(value.length < 3){
					value = value.replace(/[^0-9]/g,'');
					temp1 = obj.value;
				}else{
					temp1 = value.substring(0, 3);
					temp1 = temp1.replace(/[^0-9]/g,'');
					if(value.length >= 4){
						temp2 = value.substring(3, 4);
						temp2 = temp2.replace(/[^\/]/g,'');
					}
					if(value.length >= 5 ){
						if(value.length ==5 ){
							temp3 = value.substring(4, 5);
						}else{
							temp3 = value.substring(4, 6);
						}
						temp3 = temp3.replace(/[^0-9]/g,'');
					}
					if(value.length >=7 ){
						temp4 = value.substring(5, 7);
						temp4 = temp4.replace(/[^\/]/g,'');
					}
					if(value.length > 7 ){
						temp5 = value.substring(7);
						temp5 = temp5.replace(/[^0-9]/g,'');
					}
				}
				value = temp1 + temp2 + temp3 + temp4 + temp5 ;
				obj.value = value;

				if(value.length >= 3){
					if(value.length == 3){
						obj.value = value + dateSplit;
						return;
					}
					if(value.length == 6){
						obj.value = value + dateSplit;
						return;
					}
				}
			}else{
				return;
			}
		}
		return;
	};

	/**
	 * <b>檢查日期是否輸入正確</b><br/>
	 * 使用方法：onchange="DateUtil.checkDate(this, 'AD')"
	 * @param	calendarObj	日期型態, 可分為AD(西元)或ROC(民國)
	 * @param	dateType	所檢查物件
	 */
	this.checkDateConstraint = function (calendarObj, dateType, dateSplit){
		if(calendarObj.value != null && calendarObj.value != ""){
			var checkDateValue=calendarObj.value;
			var year;
			var month;
			var day;
			num = /\D/; 
			checkDateValue=checkDateValue.replace(/\//g,"");
			if(num.test(checkDateValue)){
				alert('您所輸入的日期型態有誤，請輸入正確日期!!!');
				calendarObj.value="";
				return false;
			}
			if(dateType=="ROC"){
				if(checkDateValue.length==6){
					checkDateValue="0"+checkDateValue;
				}else if(checkDateValue.length>7){
					alert('請使用正確日期格式 YYY'+dateSplit+'MM'+dateSplit+'DD');
					calendarObj.value="";
					return false;
				}
				year=String(Number(checkDateValue.substr(0,3))+1911);
				month=checkDateValue.substr(3,2);
				day=checkDateValue.substr(5,2);
			}else if(dateType=="AD"){
				if(checkDateValue.length>8){
					alert('請使用正確日期格式 YYYY'+dateSplit+'MM'+dateSplit+'DD');
					calendarObj.value="";
					return false;
				}
				year=checkDateValue.substr(0,4);
				month=checkDateValue.substr(4,2);
				day=checkDateValue.substr(6,2);
			}
			var objDate = new Date(year, Number(month)-1, Number(day));
			if((Number(month) > 12) || (Number(month) < 1)){
				alert('您所輸入的月份有誤，請輸入正確日期!!!');
				calendarObj.value="";
				return false;
			}else{
				if((Number(month)!= Number((objDate.getMonth()+1)))||(Number(day) != Number(objDate.getDate()))){
					alert('您所輸入的日期有誤，請輸入正確日期!!!');
					calendarObj.value="";
					return false;
				}
			}
			if(dateType=="AD"){
				calendarObj.value=year+dateSplit+month+dateSplit+day;
			}else if(dateType=="ROC"){
				year=String(Number(year)-1911);
				if(year.length==2){
					year="0"+year;
				}
				calendarObj.value=year+dateSplit+month+dateSplit+day;
			}
		}
	};
	
	/**
	 * <b>檢查傳入值是否為整數</b><br/>
	 * @param	checkInt 欲判斷之數值
	 * @return	boolean值
	 */
	var isInteger = function (checkInt){
		var result = false;
		if((!isNaN(checkInt)) && ((checkInt - parseInt(checkInt, 10)) == 0) && ((checkInt + "").indexOf(".") == -1)){
			result = true;
		}
		return result;
	};
	
	/**
	 * <b>左補字串</b>
	 * @param 	origStr	原本字串
	 * @param 	length	指定長度
	 * @param 	newWord 補充字元
	 * @return 	{String}
	 */
	var lPad = function (origStr, length, newWord) {
		var result = "";
		if (length > 0) {
			if (origStr.length < length) {
				var i = 0;
				
				while (result.length < (length - origStr.length)) {
					if (i >= newWord.length) {
						i = 0;
					}
					result += newWord.substring(i, 1);
					i++;
				}
			}
			result += origStr;
			return result;
		}else{
			return result;
		}
	};
	
	/**
	 * <b>javascript replaceAll</b>
	 * @param	origStr	原先字串
	 * @param	target	指定目標
	 * @param	replacement 更換字串
	 * @return	{String}
	 */
	var replaceAll = function (origStr, target, replacement){
		var index = 0;
		while (origStr.indexOf(target, index) != -1){
			origStr = origStr.replace(target, replacement);
			index = origStr.indexOf(target, index);
		}
		return origStr;
	};

};

var DateUtils = new DateUtils();

//為了Firefox可取得keyCode
function whichKeyPress(e) {
	if (!e) {
		//if the browser did not pass the event 
		//information to the function, 
		//we will have to obtain it from the 
		//event register
		if (window.event) {
			//Internet Explorer
			e = window.event;
		} else {
			//total failure, we have no 
			//way of referencing the event
			return;
		}
	}
	if (typeof (e.keyCode) == 'number') {
		//DOM
		e = e.keyCode;
		return e;
	} else if (typeof (e.which) == 'number') {
		//NS 4 compatible
		e = e.which;
		return e;
	} else if (typeof (e.charCode) == 'number') {
		//also NS 6+, Mozilla 0.9+
		e = e.charCode;
		return e;
	} else {
		//total failure, we have no way of obtaining the key code
		return;
	}
}


